//
//  DULanguageVC.m
//  language
//
//  Created by duyujin1 on 2021/8/1.
//

#import "DULanguageVC.h"
#import "DULanguageCell.h"
#import "DULanguage.h"
#import "DUTabbarVC.h"

static NSString * const languageName = @"lName";
static NSString * const languageTag = @"lTag";

@interface DULanguageVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView * tableView;
@property (nonatomic, strong) NSArray * langugeArray;
@property (nonatomic, strong) NSString * curLanguage;

@end

@implementation DULanguageVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView=[[UITableView alloc] initWithFrame:self.view.bounds];
    _tableView.delegate=self;
    _tableView.dataSource=self;
    self.langugeArray = @[@{languageName:@"简体中文",languageTag:@"zh-Hans"}, @{languageName:@"English",languageTag:@"en"}];
    [self.tableView registerClass:[DULanguageCell class] forCellReuseIdentifier:NSStringFromClass([DULanguageCell class])];
    [self.view addSubview:self.tableView];
    
    self.navigationItem.title = NSLocalizedString(@"setting", nil);
    self.navigationItem.backBarButtonItem.title=NSLocalizedString(@"back", nil);
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.langugeArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DULanguageCell *cell =[tableView dequeueReusableCellWithIdentifier:@"id"];
    if(!cell){
        cell=[[DULanguageCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"id"];
        cell.titleLabel.text=self.langugeArray[indexPath.row][languageName];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    _curLanguage=self.langugeArray[indexPath.row][languageTag];
    [[DULanguage sharedInstance] setLanguage:_curLanguage];

    DUTabbarVC * tabbar=[[DUTabbarVC alloc] init];
    tabbar.selectedIndex=2;
    UINavigationController * nav=tabbar.selectedViewController;
    NSMutableArray * navVCs=nav.viewControllers.mutableCopy;


    DULanguageVC * lanVC=[[DULanguageVC alloc] init];
    lanVC.hidesBottomBarWhenPushed=YES;
    [navVCs addObject:lanVC];
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [UIApplication sharedApplication].delegate.window.rootViewController = tabbar;
        nav.viewControllers =navVCs;
    });
    
    
    //[UIApplication sharedApplication].keyWindow.rootViewController = vc;
}


@end
